﻿using System.Collections.Generic;
using System.Linq;
using DynamicChart.Service.FunctionConversion;
using DynamicChart.Util;

namespace DynamicChart.Service.Step.GenerateSQL;

public class GenerateWhereStep : GenerateAbstractSQLStep
{
    public GenerateWhereStep(ISqlFunctionAdapter adapter) : base(adapter)
    {
    }
    
    public override void Process(IDictionary<string, object> data, IDictionary<string, string> res)
    {
        var whereClauses = DataProcessor.EnumerateFields(data)
            .Where(t => t.Entity.ContainsKey("ScreenMode"))
            .Select(t =>
            {
                string name = t.Entity["ScreenMode"].ToString()!.ToString()!;
                string fieldName = $"{t.TableKey}.{t.ColumnKey}";
                var strategy = GetStrategy(name);
                return strategy.GenerateWhereClause(fieldName, fieldName, null, "like");
            })
            .Where(clause => !string.IsNullOrEmpty(clause))
            .ToList();

        res["where"] = string.Join(" AND ", whereClauses);
    }
}